package com.itheima.controller.backend;


import com.itheima.common.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.OrdersettingService;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
@Slf4j
@Api(tags = "统计报表")
public class ReportController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrdersettingService ordersettingService;


    /**
     * 会员数量统计
     * @return
     */
    @GetMapping(value ="/getMemberReport")
    @ApiOperation("获取会员数量统计数据")
    public Result getMemberReport() {
        log.info("获取会员统计数据");
        Map<String, Object> data = memberService.findMemberCountByMonth();

        return new Result(true,"获取会员统计数据成功",data);
    }


    /**
     * 获取套餐预约数量统计数据
     * @return
     */
    @GetMapping(value ="/getSetmealReport")
    @ApiOperation("获取套餐预约数量统计数据")
    public Result getSetmealReport() {
        log.info("获取套餐预约数量统计数据");
        Map<String, Object> data = setmealService.getSetmealReport();
        return new Result(true,"获取套餐预约数量统计数据成功",data);
    }


    @GetMapping(value ="/getBusinessReportData")
    @ApiOperation("获取运营数据")
    public Result getBusinessReportData() {
        log.info("获取运营数据");
        LocalDate reportDate = LocalDate.now(); //日期
        //会员数据统计
        Integer todayNewMember = memberService.todayNewMember(reportDate);  //今日新增会员
        Integer thisWeekNewMember = memberService.thisWeekNewMember(reportDate); //本周新成员
        Integer thisMonthNewMember = memberService.thisMonthNewMember(reportDate); //本月新增会员
        Integer totalMember = memberService.totalMember(); //总会员
        //预约数据统计
        Integer todayVisitsNumber = orderService.todayVisitsNumber(reportDate);  //今日到诊数
        Integer thisWeekVisitsNumber = orderService.thisWeekVisitsNumber(reportDate); //本周到诊数
        Integer thisMonthVisitsNumber = orderService.thisMonthVisitsNumber(reportDate); //本月到诊数
        //到诊数据统计
        Integer todayOrderNumber = ordersettingService.todayOrderNumber(reportDate); //今日预约数
        Integer thisWeekOrderNumber = ordersettingService.thisWeekOrderNumber(reportDate); //本周预约数
        Integer thisMonthOrderNumber = ordersettingService.thisMonthOrderNumber(reportDate);//本月预约数
        //热门套餐
        List<Map<String, Object>> hotSetmeal= ordersettingService.getHotSetmeal();

        Map<String, Object> data = new HashMap<>();
        data.put("reportDate",reportDate);
        data.put("todayVisitsNumber",todayVisitsNumber);
        data.put("todayNewMember",todayNewMember); //今日新增会员
        data.put("thisWeekNewMember",thisWeekNewMember);
        data.put("thisMonthNewMember",thisMonthNewMember);
        data.put("totalMember",totalMember);
        data.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        data.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        data.put("todayOrderNumber",todayOrderNumber);
        data.put("thisWeekOrderNumber",thisWeekOrderNumber);
        data.put("thisMonthOrderNumber",thisMonthOrderNumber);
        data.put("hotSetmeal",hotSetmeal);
        return new Result(true,"获取运营数据成功",data);
    }


    /**
     * 导出运营数据报表
     * @param response
     */
    @GetMapping("/exportBusinessReport")
    @ApiOperation("导出运营数据报表")
    public void export(HttpServletResponse response){
        ordersettingService.exportBusinessData(response);
    }

}
